#include "LoggerManager.h"
#include "ResourceManager.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <string>

using namespace common_component;
using namespace logger;
int main()
{
    std::cout << "=== File Server Test Program ===" << std::endl;

    // 测试Logger
    std::cout << "\n[1] Testing Logger..." << std::endl;
    try
    {
        LogConfig config = {"file_back_end"};
        LoggerManager::getInstance().init(config);
        LOG_INFO << "Logger initialized successfully";
        LOG_DEBUG << "This is a debug message";
        LOG_WARN << "This is a warning message";
        LOG_ERROR << "This is an error message";
        std::cout << "✓ Logger test passed" << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << "✗ Logger test failed: " << e.what() << std::endl;
        return 1;
    }

    // 测试ResourceManager和ThreadPool
    std::cout << "\n[2] Testing ResourceManager & ThreadPool..." << std::endl;
    try
    {
        auto &manager = ResourceManager::Instance();

        // 创建线程池
        auto pool1 = manager.CreateThreadPool("worker_pool", 4);
        auto pool2 = manager.CreateThreadPool("io_pool", 2);

        // 提交任务到不同的线程池
        std::vector<std::future<int>> results;

        // worker_pool任务
        for (int i = 0; i < 4; ++i)
        {
            // results.emplace_back(
            //     pool1->enqueue([i]
            //                    {
            //         std::this_thread::sleep_for(std::chrono::milliseconds(100));
            //         LOG_INFO << "Worker task " << i << " completed";
            //         return i * i; }));
            results.emplace_back(pool1->Submit([i]()
                                               {
                std::this_thread::sleep_for(std::chrono::milliseconds(100));
                LOG_INFO << "Worker task " << i << " completed";
                return i * i; }));
        }

        // io_pool任务
        for (int i = 0; i < 2; ++i)
        {
            results.emplace_back(
                pool2->Submit([i]
                              {
                    std::this_thread::sleep_for(std::chrono::milliseconds(50));
                    LOG_INFO << "IO task " << i << " completed";
                    return (i + 10) * 2; }));
        }

        // 等待所有任务完成
        for (size_t i = 0; i < results.size(); ++i)
        {
            int result = results[i].get();
            std::cout << "  Task " << i << " result: " << result << std::endl;
        }

        std::cout << "✓ ThreadPool test passed" << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << "✗ ThreadPool test failed: " << e.what() << std::endl;
        return 1;
    }

    // 测试ObjectPool
    std::cout << "\n[3] Testing ObjectPool..." << std::endl;
    try
    {
        auto &manager = ResourceManager::Instance();

        // 创建字符串对象池
        auto string_pool = manager.CreateObjectPool<std::string>("string_pool", 5, 20, 3);

        // 获取对象
        std::vector<std::shared_ptr<std::string>> strings;
        for (int i = 0; i < 3; ++i)
        {
            auto str = string_pool->acquire("Hello_" + std::to_string(i));
            strings.push_back(str);
            std::cout << "  Got string: " << *str << std::endl;
        }

        // 获取统计信息
        auto stats = string_pool->getStatistics();
        std::cout << "  Pool stats - allocated: " << stats.allocated_count
                  << ", hit_rate: " << (stats.hit_rate * 100) << "%" << std::endl;

        std::cout << "✓ ObjectPool test passed" << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << "✗ ObjectPool test failed: " << e.what() << std::endl;
        return 1;
    }

    // 显示资源统计
    std::cout << "\n[4] Resource Statistics..." << std::endl;
    try
    {
        auto &manager = ResourceManager::Instance();
        manager.PrintStatistics();

        std::cout << "✓ Statistics test passed" << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << "✗ Statistics test failed: " << e.what() << std::endl;
        return 1;
    }

    std::cout << "\n=== All tests passed! ===" << std::endl;

    // 清理资源
    ResourceManager::Instance().Shutdown();

    // 等待日志刷新
    std::this_thread::sleep_for(std::chrono::seconds(1));

    return 0;
}
